package com.lysj.bhp.borrowing.controller;

import com.lysj.bhp.borrowing.base.BorrowingContent;
import com.lysj.bhp.borrowing.domain.BorrowingRecord;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.withdraw.domain.WithDraw;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: ZhangWenJian
 * @Create: 2018/8/17--11:46
 * @Description: 借贷申请记录管理控制类
 */
@RestController
@RequestMapping("admin/borrowing_apply_record")
public class BorrowingApplyRecordController extends BorrowingContent {

    @RequestMapping("/list")
    public Resp list() {
        List<BorrowingRecord> borrowingRecordList = borrowingRecordRepository.findByStatusAndCheckStatusNotInOrderByApplyDateDesc(1, -1);
        return new Resp(borrowingRecordList);
    }


    @PostMapping("/approval")
    public Resp approval(BorrowingRecord model) {
        BorrowingRecord borrowingRecord = borrowingRecordRepository.findOne(model.getBorrowingRecordId());
        if (borrowingRecord == null) {
            return new Resp(Resp.PARAM_ERROR, "该申请信息已被修改!");
        }
        return borrowingRecordService.borrowingRecordApproval(borrowingRecord,model.getCheckStatus());
    }

    @PostMapping("/make_loans")
    public Resp makeLoans(BorrowingRecord model) throws Exception {
        if (ParamUtil.isBlack(model.getBorrowingRecordId())) {
            return new Resp(Resp.PARAM_ERROR, "请选择借款记录");
        }
        BorrowingRecord borrowingRecord = borrowingRecordRepository.findOne(model.getBorrowingRecordId());
        if (null == borrowingRecord) {
            return new Resp(Resp.PARAM_ERROR, "查无此记录,请联系管理员");
        }
        if (model.getCheckStatus() == 3) {
            return loanMoneyService.loadMoney(borrowingRecord);
        } else {
            return borrowingRecordService.borrowingRecordApproval(borrowingRecord,model.getCheckStatus());
        }
    }


}
